package com.ysstech.finance.controller;


import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.finance.entity.Account;
import com.ysstech.finance.entity.Balance;
import com.ysstech.finance.service.FinAccountService;
import com.ysstech.finance.service.BalanceService;
import com.ysstech.finance.thread.BalanceMainGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-04-01
 */
@Controller
@Slf4j
@Api(tags = "余额表管理api")
@RequestMapping("/balance")
public class BalanceController extends BaseController {
    @Autowired
    private BalanceService balanceService;
    @Autowired
    private FinAccountService finAccountService;
    @Autowired
    private BalanceMainGenerator balanceMainGenerator;

    @YssPermissions(value = {"finance:balance:balance"})
    @ApiOperation(value = "跳转余额表管理页面")
    @YssLog(module = "余额表管理", remark = "跳转余额表管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "finance/balance";
    }


    @YssPermissions(value = {"finance:balance:balance"})
    @ApiOperation(value = "获取余额表页面初始值")
    @YssLog(module = "余额表管理", remark = "获取余额表页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        Account account = new Account();
        List<TreeNode> lsetlists = finAccountService.queryAccountTree(account);
        map.put("lsetlists", lsetlists);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }


    /**
     * 获取余额表信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:balance:balance"})
    @ApiOperation(value = "获取余额表信息")
    @YssLog(module = "余额表管理", remark = "获取余额表信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(Balance balance, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取余额表信息成功",
                balanceService.queryAllBalance(balance, this.getPage(page, rows)));
    }

    /**
     * 生成余额表信息
     *
     * @return
     */
    @YssPermissions(value = {"finance:balance:balance:produce"})
    @ApiOperation(value = "生成余额表信息")
    @YssLog(module = "余额表管理", remark = "生成余额表信息")
    @PostMapping(value = "/produceBalance")
    @ResponseBody
    public RestResult produceBalance(Balance balance) throws Exception {
        if (StringUtils.isBlank(balance.getAccountId())) {
            throw new BusinessException("获取基金信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(balance.getAccountId(), String.class);
        balanceMainGenerator.renewBalance(balance, list);
        return ResultGenerator.getSuccessResult("生成余额表信息成功");
    }

    @ApiOperation(value = "查询明细的余额表数据")
    @YssLog(module = "余额表管理", remark = "查询明细的余额表数据")
    @PostMapping(value = "/queryBalanceByValuation")
    @ResponseBody
    public List<Balance> queryBalanceByValuation(String accountId, String fdate) throws Exception {
        return balanceService.queryBalanceByValuation(accountId, DateUtil.strToLocalDate(fdate));
    }


}
